package com.cloudsigns.factorymanager2.project.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.BaseActivity;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.engineering.bean.AccountInfoBean;
import com.cloudsigns.factorymanager2.produce.iview.IGetAccountListView;
import com.cloudsigns.factorymanager2.produce.presenter.GetAccountListPresenter;
import com.cloudsigns.factorymanager2.project.adapter.PersonnelSettingAdapter1;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.List;

public class PersonnelSettingListActivity1 extends BaseActivity implements IGetAccountListView {
    private String TAG = PersonnelSettingListActivity1.class.getSimpleName();
    private RelativeLayout rl_right;
    private ImageView img_back;
    private TextView tv_title;
    private ListView lv;

    private List<AccountInfoBean> accountItemList;

    private GetAccountListPresenter getAccountListPresenter;

    private PersonnelSettingAdapter1 adapter;


    @Override
    protected void onBaseCreate(Bundle savedInstanceState) {
        setContentView(R.layout.layout_personnel_setting_list);


    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        img_back = findViewById(R.id.img_back);
        rl_right = findViewById(R.id.rl_right);
        tv_title = findViewById(R.id.tv_title);
        lv = findViewById(R.id.lv);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        tv_title.setText(getResources().getString(R.string.account_manager));

        adapter = new PersonnelSettingAdapter1(this, TAG);

        getAccountListPresenter = new GetAccountListPresenter(this);
        getAccountListPresenter.getAccountList();

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(PersonnelSettingListActivity1.this, PersonnelSettingPermissionActivity.class);
                startActivity(intent);
            }
        });

        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long l) {
                AccountInfoBean accountInfoBean = accountItemList.get(position);
                LayoutInflater inflater = getLayoutInflater();
                View view1 = inflater.inflate(R.layout.dialog_delete, null);
                Button btn_cancle = view1.findViewById(R.id.btn_cancel);
                Button btn_sub = view1.findViewById(R.id.btn_submit);
                TextView tv_content = view1.findViewById(R.id.tv_content);
                final Dialog dialog = new Dialog(PersonnelSettingListActivity1.this);
                Context context = dialog.getContext();
                try {
                    int divierId = context.getResources().getIdentifier("android:id/titleDivider", null, null);
                    View divider = dialog.findViewById(divierId);
                    divider.setBackgroundColor(Color.TRANSPARENT);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                dialog.show();
                Window window = dialog.getWindow();
                WindowManager.LayoutParams lp = window.getAttributes();
                window.setBackgroundDrawable(new ColorDrawable(0));
                window.setGravity(Gravity.CENTER);
                window.setAttributes(lp);
                dialog.setContentView(view1);
                tv_content.setText("确定删除【" + accountInfoBean.getAccountName() + "】的账号吗？");

                // 取消
                btn_cancle.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                    }
                });

                // 确定
                btn_sub.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();

                    }
                });
                return true;
            }
        });
    }

    @Override
    public void setListener() {
        img_back.setVisibility(View.VISIBLE);
        rl_right.setVisibility(View.VISIBLE);
        img_back.setOnClickListener(this);
        rl_right.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View view) {
        Intent mIntent;
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            // 创建账户
            case R.id.rl_right:
                mIntent = new Intent(PersonnelSettingListActivity1.this, AddPersonnelSettingActivity1.class);
                startActivityForResult(mIntent, ConstantUtil.ADD_PERSONNEL_SETTING);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case ConstantUtil.ADD_PERSONNEL_SETTING: //添加人员设置
                if (data != null) {
                    getAccountListPresenter.getAccountList();
                    adapter.setData(accountItemList);
                    adapter.notifyDataSetChanged();
                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void getAccountListSuccessed(List<AccountInfoBean> accountItemList) {
        if (null != accountItemList) {
            this.accountItemList = accountItemList;
            adapter.setData(accountItemList);
            lv.setAdapter(adapter);
        }
    }

    @Override
    public void getAccountListFailed(final String desc) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast(desc);
            }
        });
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
